#ifndef  __PL2219_H__
#define __PL2219_H__

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

/* Debug */
#define DEBUG                   0

/* PL2219_I2C_ADDR I2C address */
#define PL2219_I2C_ADDR       ((0x48))//(0x48)



#define  PROXIMITY_THREASOLD    95
#define  COLOR_THREASOLD        20




#define  AIR_PROXIMITY_THREASOLD   				  5   //完全悬空状态值

#define WHILE_PROXIMITY_THREASOLD      			  200 
#define WHILE_MOVE_PROXIMITY_THREASOLD      	  10 


#define MUTATION_THREASOLD                        50  //突变阈值

#define BLACK_PROXIMITY_THREASOLD                 50 
#define BLACK_MOVE_PROXIMITY_THREASOLD      	  10 


#define  DISTANCE_5CM_WHILE_PROXIMITY_THREASOLD   5   //悬空5cm底部白色桌面状态值
#define  DISTANCE_5CM_BLACK_PROXIMITY_THREASOLD   5   //悬空5cm底部黑色桌面状态值




#define MUTATION_COOLDOWN_TIME 100       // 突变冷却时间，单位为毫秒，防止重复触发突变


// 状态定义
typedef enum{

 STATE_BLACK_SURFACE      = 0,      //黑色桌面接近
 STATE_BLACK_SURFACE_AIR  = 1,      //黑色桌面远离
 STATE_NEAR_OBJECT        = 2,      //白色桌面接近
 STATE_NEAR_OBJECT_AIR    = 3,      //白色桌面远离
 STATE_IN_AIR             = 4,      //悬空状态
 STATE_IDLE               = 5,      //空闲状态

}State;

/*
	总共9种状态
	STATE_BLACK_SURFACE ---> STATE_BLACK_SURFACE
	STATE_BLACK_SURFACE ---> STATE_IN_AIR
	STATE_BLACK_SURFACE ---> STATE_NEAR_OBJECT

	STATE_NEAR_OBJECT ---> STATE_NEAR_OBJECT
	STATE_NEAR_OBJECT ---> STATE_BLACK_SURFACE
	STATE_NEAR_OBJECT ---> STATE_IN_AIR

	STATE_IN_AIR      ---> STATE_IN_AIR
	STATE_IN_AIR      ---> STATE_BLACK_SURFACE
	STATE_IN_AIR      ---> STATE_NEAR_OBJECT
*/


typedef struct{

	  uint8_t id_num;
	  bool is_init;
	  bool status;
	  bool last_status;
		bool is_black_desktop;
	
		uint8_t proximity_data;
	
		uint16_t ambient_light;
		uint16_t red_light;
		uint16_t green_light;
		uint16_t blue_light;
	
	
		uint16_t pins;
	  void (*Set_Out)(uint16_t pins,bool level);  // ��������״̬



	bool state_is_init;
	uint8_t scale_proximity;
	uint16_t proximity;
	uint8_t last_scale_proximit;
    uint32_t current_state;           // 当前状态
	uint32_t last_state;              // 上一次的状态
	uint8_t  count; 
	bool is_deal;
	uint8_t  count1; 
	bool is_deal1;

	bool hold_on;    //保持状态
	bool is_stable;  //是否稳定
	bool status_update;  //状态更新
	unsigned long current_hold_on_time;
	unsigned long last_hold_on_time;

	
}_PL2219_t;



typedef struct{

	 uint8_t i2c_id;
	_PL2219_t PL2219[4];

}PL2219_t;
;

void PL2219_Init();
void PL2219_Task();

	

    /* Raw I2C Commands */
    static bool wireWriteByte(uint8_t val);
    static bool wireWriteDataByte(uint8_t reg, uint8_t val);
    static bool wireWriteDataBlock(uint8_t reg, uint8_t *val, unsigned int len);
    static bool wireReadDataByte(uint8_t reg, uint8_t *val);
    static int wireReadDataBlock(uint8_t reg, uint8_t *val, unsigned int len);





// 1.悬崖方案软件调试
//       1.APDS9960 方案  接近+颜色
//    2.AP3216c    方案  接近+环境光
//    3.STK36C61  方案  接近+光谱颜色
//    4.PL2219      方案  接近
  
// 2.新方案软件验证及功能编写

// 3.运动控制优化

// 4.蓝牙广播与灯效逻辑







#endif